﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;


namespace ZBZH_Manage_Order.Controllers.TakerMangement
{
    public class TakerSendController : Controller
    {
        //
        // GET: /TakerSend/

        //连接服务
        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();
        public ActionResult Index()
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                int OperID = user.OperID;
                ViewData["OperID"] = OperID;
                return View();
            }
            catch
            {            
               return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面               
            }
        }

        // 加载商户
        public ActionResult InitMerID(JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_Mer_T> page = new Page<Model.Order_Mer_T>();
                List<Model.Order_Mer_T> list = new List<Model.Order_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = wsdcser.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Model.Order_Mer_T m = new Model.Order_Mer_T();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            m.MerAdr = item["MerAdr"].ToString();
                            m.MerCall = item["MerCall"].ToString();
                            m.MerTxt = item["MerTxt"].ToString();
                            list.Add(m);
                        }
                    }
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 根据条件获取派送员订单管理
        /// </summary>
        /// <param name="Merid"></param>
        /// <param name="Takerid"></param>
        /// <param name="Formid"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult View_TakerForm(string Merid, string TakerName, string Formid, string stime, string etime, JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.View_Order_Send_T> page = new Page<Model.View_Order_Send_T>();
                string s="";
                string e = "";
                if (stime != null && stime!="")
                {
                    s = stime.Substring(0, 4) + stime.Substring(5, 2) + stime.Substring(8,2);
                }
                if (etime != null && etime != "")
                {
                     e = etime.Substring(0, 4) + etime.Substring(5, 2) + etime.Substring(8, 2);
                }
                List<Model.View_Order_Send_T> no = new List<Model.View_Order_Send_T>();
                List<Model.View_Order_Send_T> list = new List<Model.View_Order_Send_T>();
                
                string result = wsdcser.GetGrid_Wiew_Send(jqgridparam.page, jqgridparam.rows, Merid, TakerName, Formid, s, e, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();
                    if (jieguo.Count > 0)
                    {
                        
                        Model.View_Order_Send_T mo = new Model.View_Order_Send_T();
                        no = ParseClass<Model.View_Order_Send_T>.JsonList(jieguo);
                        foreach (var it in no) {
                            mo = it;
                            mo.FormPayMon = mo.FormPayMon / 100;
                            list.Add(mo);
                        }
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);//, JsonRequestBehavior.AllowGet
                }
                else
                {
                    var data = new
                    {
                        total = "0",
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }

        }

    }
}
